package com.soft.base.protocol;


import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
/**
 * class
 *
 * @author user
 * @date 2020/06/05
 */
public class PaginateResult<T> {

    @Getter
    @Setter
    private long page;

    @Getter
    @Setter
    private long pageSize;

    @Getter
    @Setter
    private long total;

    @Getter
    @Setter
    private List<T> records;

    public static interface Convertor<T, TO> {
        /**
         * convert
         * @param o
         * @return
         */
        TO convert(T o);
    }

    public static <TO, T> PaginateResult<TO> convert(PaginateResult<T> original, Convertor<T, TO> convertor) {
        PaginateResult<TO> result = new PaginateResult<>();
        result.setPage(original.getPage());
        result.setPageSize(original.getPageSize());
        result.setTotal(original.getTotal());
        List<TO> records = new ArrayList<>(original.getRecords().size());
        for (T t : original.getRecords()) {
            records.add(convertor.convert(t));
        }
        result.setRecords(records);
        return result;
    }

}

